﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace GraphicsEditor
{
    public class PixelClick
    {

        /*
         * Gets the pixel location 
         * 0,0 is the upper left pixel 
         * 
         */ 
        protected Point getPixelPositionUsingMouse(Point mousePoint, PictureBoxAdvance pictureWindow)
        {

            //since 0,0 is always in the upper left-hand corner, its just a matter of finding
            //these ratios.
            if (mousePoint.X <= 0)
                mousePoint.X = 0;
            if (mousePoint.Y <= 0)
                mousePoint.Y = 0;

            if (mousePoint.X >= pictureWindow.Width)
                mousePoint.X = pictureWindow.Width-1;
            if (mousePoint.Y >= pictureWindow.Height)
                mousePoint.Y = pictureWindow.Height-1;

            float widthRatio = (float)pictureWindow.Width / pictureWindow.Image.Width;
            float heightRatio = (float)pictureWindow.Height / pictureWindow.Image.Height;

            Point point = new Point();
            point.X = (int)Math.Floor(mousePoint.X / widthRatio);
            point.Y = (int)Math.Floor(mousePoint.Y / heightRatio);

            return point;
        }

        protected Point getCanvasPositionUsingPixel(Point pixelPoint, PictureBoxAdvance pictureWindow)
        {

            //since 0,0 is always in the upper left-hand corner, its just a matter of finding
            //these ratios.
            if (pixelPoint.X <= 0)
                pixelPoint.X = 0;
            if (pixelPoint.Y <= 0)
                pixelPoint.Y = 0;
                
            if (pixelPoint.X >= pictureWindow.Width)
                pixelPoint.X = pictureWindow.Width - 1;
            if (pixelPoint.Y >= pictureWindow.Height)
                pixelPoint.Y = pictureWindow.Height - 1;

            float widthRatio = (float)pictureWindow.Width / pictureWindow.Image.Width;
            float heightRatio = (float)pictureWindow.Height / pictureWindow.Image.Height;

            Point point = new Point();
            point.X = (int)Math.Floor(pixelPoint.X * widthRatio);
            point.Y = (int)Math.Floor(pixelPoint.Y * heightRatio);

            return point;
        }

        /*
         * Gets the position of the upper left corner
         * of the pixel, this is helpful when drawing while
         * zoomed in or out.
         * 
         */
        protected Point getUpperLeftPixelCorner(Point p, PictureBoxAdvance pictureWindow)
        {
            Point result = new Point(0, 0);
            if (p.X < 0)
                p.X = 0;
            if (p.Y < 0)
                p.Y = 0;

            if (p.X > pictureWindow.Image.Width)
                p.X = pictureWindow.Image.Width;
            if (p.Y > pictureWindow.Image.Height)
                p.Y = pictureWindow.Image.Height;

            int imageWidth = pictureWindow.Image.Width;
            float pixelsize = (float)pictureWindow.Width / imageWidth;

            result.X = (int)(p.X * pixelsize);
            result.Y = (int)(p.Y * pixelsize);
            if (result.X == 0)
                result.X = 1;
            if (result.Y == 0)
                result.Y = 1;
            return result;
        }

        /*
         * Gets the position of the upper right corner
         * of the pixel, this is helpful when drawing while
         * zoomed in or out.
         * 
         */
        protected Point getUpperRightPixelCorner(Point p, PictureBoxAdvance pictureWindow)
        {
            Point result = new Point(0, 0);
            if (p.X < 0)
                p.X = 0;
            if (p.Y < 0)
                p.Y = 0;

            if (p.X > pictureWindow.Image.Width)
                p.X = pictureWindow.Image.Width;
            if (p.Y > pictureWindow.Image.Height)
                p.Y = pictureWindow.Image.Height;

            int imageWidth = pictureWindow.Image.Width;
            float pixelsize = (float)pictureWindow.Width / imageWidth;

            result.X = (int)((p.X * pixelsize) + pixelsize);
            result.Y = (int)(p.Y * pixelsize);
            if (result.X == 0)
                result.X = 1;
            if (result.Y == 0)
                result.Y = 1;

            return result;
        }

        /*
         * Gets the position of the lower left corner
         * of the pixel, this is helpful when drawing while
         * zoomed in or out.
         * 
         */
        protected Point getLowerLeftPixelCorner(Point p, PictureBoxAdvance pictureWindow)
        {
            Point result = new Point(0, 0);
            if (p.X < 0)
                p.X = 0;
            if (p.Y < 0)
                p.Y = 0;

            if (p.X > pictureWindow.Image.Width)
                p.X = pictureWindow.Image.Width;
            if (p.Y > pictureWindow.Image.Height)
                p.Y = pictureWindow.Image.Height;

            int imageWidth = pictureWindow.Image.Width;
            float pixelsize = (float)pictureWindow.Width / imageWidth;

            result.X = (int)(p.X * pixelsize);
            result.Y = (int)((p.Y * pixelsize) + pixelsize);
            if (result.X == 0)
                result.X = 1;
            if (result.Y == 0)
                result.Y = 1;

            return result;
        }

        /*
         * Gets the position of the lower right corner
         * of the pixel, this is helpful when drawing while
         * zoomed in or out.
         * 
         */
        protected Point getLowerRightPixelCorner(Point p, PictureBoxAdvance pictureWindow)
        {
            Point result = new Point(0, 0);
            if (p.X < 0)
                p.X = 0;
            if (p.Y < 0)
                p.Y = 0;

            if (p.X > pictureWindow.Image.Width)
                p.X = pictureWindow.Image.Width;
            if (p.Y > pictureWindow.Image.Height)
                p.Y = pictureWindow.Image.Height;

            int imageWidth = pictureWindow.Image.Width;
            float pixelsize = (float)pictureWindow.Width / imageWidth;
            //this simplistic scaling will only work for now,
            //currently if you zoom out too far
            result.X = (int)((p.X * pixelsize) + pixelsize);
            result.Y = (int)((p.Y * pixelsize) + pixelsize);
            if (result.X == 0)
                result.X = 1;
            if (result.Y == 0)
                result.Y = 1;

            return result;
        }

        /*
         * When drawing rectangles you start with the upper left hand corner and
         * end with the lower right hand corner, this function will give you
         * the upper left and lower right points for any two given points.
         * 
         */
        protected Point[] getRect(Point a, Point b)
        {
            Point start = new Point(0, 0), end = new Point(0, 0);
            if (a.X >= b.X)
            {
                start.X = b.X;
                end.X = a.X;
            }
            if (a.X < b.X)
            {
                start.X = a.X;
                end.X = b.X;
            }
            if (a.Y < b.Y)
            {
                start.Y = a.Y;
                end.Y = b.Y;
            }
            if (a.Y >= b.Y)
            {
                start.Y = b.Y;
                end.Y = a.Y;
            }
            return new Point[] { start, end };
        }

        protected Rectangle getRectFromPoints(Point p1, Point p2)
        {
            int x;
            int y;

            if (p1.X <= p2.X)
            {
                x = p1.X;
            }
            else
            {
                x = p2.X;
            }

            if (p1.Y <= p2.Y)
            {
                y = p1.Y;
            }
            else
            {
                y = p2.Y;
            }

            return new Rectangle(x, y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y));
        }
    }
}
